package com.itheima.功能流.序列化流;

import java.io.*;
import java.util.ArrayList;

public class 序列化流的终止 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ArrayList<User> lists = new ArrayList<>();
        lists.add(new User("zhangsan","123456"));
        lists.add(new User("李四","qwer"));
        lists.add(new User("wangwu","wedc"));
        lists.add(new User("zhaoliu","admin"));
        lists.add(new User("qianqi","root"));
        lists.add(new User("zhaoba","tester"));
        method2(lists);
        method1();


    }

    private static void method2(ArrayList<User> lists) throws IOException {
        //创建对象输入流
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.sql"));

        //循环读写
        for (User user : lists) {
            oos.writeObject(user);
        }

        oos.close();
    }

    private static void method1() throws IOException, ClassNotFoundException {
        //创建对象输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("user.sql"));

        //循环读写
        while (true){
            try{
                Object o = ois.readObject();
                System.out.println(o);
            }catch (IOException e){
                break;
            }
        }

        ois.close();
    }
}
